{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "library(IRdisplay)\n",
    "library(IRkernel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "target_name_in_kernel  <- 'kernel_target'\n",
    "target_name_in_browser <- 'browser_target'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Open handler in the kernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "target_handler <- function(comm, data) {\n",
    "    cat('Got open call\\n')\n",
    "    print(data)\n",
    "    \n",
    "    comm$on_msg(function(msg) {\n",
    "        cat('Got message\\n')\n",
    "        print(msg)\n",
    "    })\n",
    "    \n",
    "    comm$on_close(function() {\n",
    "        cat('Got close\\n')\n",
    "    })\n",
    "}\n",
    "\n",
    "comm_manager()$register_target(target_name_in_kernel, target_handler)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test in Javascript console"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "display_javascript(sprintf('\n",
    "const comm = IPython.notebook.kernel.comm_manager.new_comm(\"%s\", {list: [1,2,3,4,5,6,7,8,9]})\n",
    "\n",
    "comm.send({foo: \"bar\"})\n",
    "\n",
    "comm.close()\n",
    "', target_name_in_kernel))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Open handler in browser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Register target in browser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "display_javascript(sprintf('\n",
    "IPython.notebook.kernel.comm_manager.register_target(\"%s\", () =>\n",
    "    console.log(\"Got open message\", arguments))\n",
    "', target_name_in_browser))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "comm <- comm_manager()$new_comm(target_name_in_browser)\n",
    "comm$open()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Need to get a handle on the comm object in the browser.  \n",
    "Put a breakpoint in comm.js in CommManager.prototype.comm_open and run `window.testcomm = comm`\n",
    "\n",
    "Register on_msg and on_close:\n",
    "* `window.testcomm.on_msg(function(data){console.log(\"Got data\", data)})`\n",
    "* `window.testcomm.on_close(function(data){console.log(\"Got close\", data)})`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "comm$send()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "comm$close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.2.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
